import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import '../../routes/Routes.dart';
import 'EndWorkNextOneController.dart';

class EndWorkNextOnePage extends StatelessWidget {
  final logic = Get.put(EndWorkNextOneController());
  final state = Get.find<EndWorkNextOneController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                SubTitleBar("结束作业", '下一步', () {
                  logic.next();
                }),
                TextView("批次ID:", state.lotEntity?.lOTID ?? ""),
                TextView("设备ID:", state.lotEntity?.eQUIPMENTID ?? ""),
                TextView("进站数量:", state.lotEntity?.mAINQTY.toString() ?? "0"),
                TextView("当前工步:", (state.lotEntity?.sTEPNAME ?? "") + '/' + (state.lotEntity?.sTEPDESC ?? "")),
                TitleEditText("出站数量:", "", (value){
                    state.lotEntity?.mAINQTY = value.toDouble();
                },  type: TextInputType.number,
                  isScan: false,
                  controller: state.qtyController,),
                Container(
                  margin: EdgeInsets.all(8),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "载具列表",
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                ),
                GetBuilder<EndWorkNextOneController>(builder: (logic) => Column(
                  children: [
                    ClearEditText("请扫描载具ID", (value) {
                      logic.addCarryData(value);
                    },controller: TextEditingController(text: ''),),
                  ],
                ),id: state.carrierLogicId,),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<EndWorkNextOneController>(
                    builder: (logic) => Table(
                          defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(8.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getCarryList(context),
                        ),
                    id: state.carryLogicId),
                Container(
                  margin: EdgeInsets.all(8),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "录入不良信息",
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                ),
                GetBuilder<EndWorkNextOneController>(
                  builder: (logic) => Align(
                    alignment: Alignment.center,
                    child: Row(
                      children: [
                        SizedBox(
                          width: 12,
                        ),
                        Text(
                          "不良代码",
                          style: TextStyle(
                            color: text_black.toColor(),
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Expanded(
                            flex: 1,
                            child: Container(
                              padding: EdgeInsets.only(left: 4),
                              height: 36,
                              decoration: BoxDecoration(
                                  border: new Border.all(
                                      width: 1.0, color: theme_blue.toColor()),
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(4))),
                              child: DropdownButtonHideUnderline(
                                child: DropdownButton(
                                  value: state.selectDefectValue,
                                  items: getList(),
                                  onChanged: (value) {
                                    logic.updateSelectDefectValue(
                                        value as LotSCRAPCODELIST);
                                  },
                                  hint: Text(
                                    "请选择不良代码",
                                    style: TextStyle(
                                      fontSize: 14,
                                    ),
                                  ),
                                ),
                              ),
                            )),
                        SizedBox(
                          width: 8,
                        ),
                        Container(
                          height: 36,
                          margin: EdgeInsets.only(right: 8),
                          child: ElevatedButton(
                            onPressed: () {
                              logic.addDefectData();
                            },
                            child: Text("保存"),
                          ),
                        ),
                        SizedBox(
                          width: 4,
                        ),
                      ],
                    ),
                  ),
                  id: state.defectCodeLogicId,
                ),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<EndWorkNextOneController>(
                    builder: (logic) =>
                        TextView("不良数量", state.defectCount.toString()),
                    id: state.defectCountLogicId),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<EndWorkNextOneController>(
                    builder: (logic) => Table(
                          defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(2.0),
                            2: FlexColumnWidth(1.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getDefectList(context),
                        ),
                    id: state.defectTableLogicId),
              ],
            ),
          ),
        ));
  }

  List<TableRow> getCarryList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '序号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '载具编号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.carryList.length; i++) {
      String element = state.carryList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              "${i + 1}",
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Row(
            children: [
              SizedBox(
                width: 8,
              ),
              Expanded(
                  flex: 1,
                  child: Text(
                    element,
                    style: TextStyle(
                      color: text_black.toColor(),
                    ),
                  )),
              IconButton(
                icon: Icon(
                  Icons.delete,
                  color: Colors.red,
                ),
                onPressed: () {
                  logic.deleteCarryData(element);
                },
              )
            ],
          ),
        ),
      ]));
    }
    return tempView;
  }

  List<TableRow> getDefectList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '不良代码',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '不良描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.defectList.length; i++) {
      LotSCRAPCODELIST element = state.defectList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.cODE ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.dESCRIPTION ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "",
              (value) {
                if (value.isEmpty) {
                  value = "0";
                }
                element.DEFECTQTY = value;
                logic.updateDefectCount();
              },
              type: TextInputType.number,
              isScan: false,isShowIcon: false,
              controller: TextEditingController(text: element.DEFECTQTY),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }

  List<DropdownMenuItem<LotSCRAPCODELIST>> getList() {
    List<DropdownMenuItem<LotSCRAPCODELIST>> mList = [];
    state.scrapCodeList.forEach((element) {
      mList.add(DropdownMenuItem(
        child: Text(element.dESCRIPTION ?? ''),
        value: element,
      ));
    });
    return mList;
  }
}
